import 'package:flutter/material.dart';
import 'package:play_android/com/zhihaoliang/base/BaseState.dart';
import 'package:play_android/com/zhihaoliang/const/AppStr.dart';
import 'package:play_android/com/zhihaoliang/http/HttpException.dart';
import 'package:play_android/com/zhihaoliang/play/article/view/ArticleDetailPage.dart';
import 'package:play_android/com/zhihaoliang/play/hotpage/model/friendsit/FriendSitItem.dart';
import 'package:play_android/com/zhihaoliang/play/hotpage/model/hotkey/HotKeyItem.dart';
import 'package:play_android/com/zhihaoliang/play/presenter/HotKeyPresenter.dart';
import 'package:play_android/com/zhihaoliang/play/presenter/FriendSitPresenter.dart';
import 'package:play_android/com/zhihaoliang/play/search/SearchPage.dart';
import 'package:play_android/com/zhihaoliang/util/ToastUtil.dart';

/// **************************************************************************
/// 热门搜索页面
/// **************************************************************************

class HotPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _HotPageState();
  }
}

class _HotPageState extends State<HotPage>
    with BaseState
    implements FriendSitCallBack, HotKeyCallBack {
  List<Widget> _hotKeyWidgets = List();
  List<Widget> _friendWidgets = List();

  @override
  void initState() {
    super.initState();

    HotKeyPresenter(this).load();
    FriendSitPresenter(this).load();
  }

  @override
  Widget build(BuildContext context) {
    return ListView(
      children: <Widget>[
        Padding(
            padding: EdgeInsets.all(10.0),
            child: Text(AppStr.HOT_KEY,
                style: TextStyle(
                    color: Theme.of(context).accentColor, fontSize: 20.0))),
        Wrap(
          spacing: 5.0,
          runSpacing: 5.0,
          children: _hotKeyWidgets,
        ),
        Padding(
            padding: EdgeInsets.all(10.0),
            child: Text(AppStr.FRIEND_SIT,
                style: TextStyle(
                    color: Theme.of(context).accentColor, fontSize: 20.0))),
        Wrap(
          spacing: 5.0,
          runSpacing: 5.0,
          children: _friendWidgets,
        ),
      ],
    );
  }

  @override
  void onLoadFriendSitComplete(List<FriendSitItem> list) {
    setState(() {
      _friendWidgets.clear();
      list.forEach((item) {
        Widget actionChip = ActionChip(
          backgroundColor: Theme.of(context).accentColor,
          label: Text(
            item.name,
            style: TextStyle(color: Colors.white),
          ),
          onPressed: () {
            Navigator.of(context).pushReplacement(MaterialPageRoute(
                builder: (context) => ArticleDetailPage(
                      title: item.name,
                      url: item.link,
                    )));
          },
        );
        _friendWidgets.add(actionChip);
      });
    });
  }

  @override
  void onLoadFriendSitError(HttpException httpException) {
    onTipError(httpException, context, AppStr.LOAD_FRIEND_SIT_FAIL);
  }

  @override
  void onLoadHotKeyComplete(List<HotKeyItem> list) {
    setState(() {
      _hotKeyWidgets.clear();
      list.forEach((item) {
        Widget actionChip = ActionChip(
          backgroundColor: Theme.of(context).accentColor,
          label: Text(
            item.name,
            style: TextStyle(color: Colors.white),
          ),
          onPressed: () {
            Navigator.of(context).pushReplacement(
                MaterialPageRoute(builder: (context) => SearchPage(item.name)));
          },
        );
        _hotKeyWidgets.add(actionChip);
      });
    });
  }

  @override
  void onLoadHotKeyeError(HttpException httpException) {
    onTipError(httpException, context, AppStr.LOAD_HOT_KEY_FAIL);
  }
}
